#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>


struct ListNode {
    int val;
    struct ListNode *next;
};

struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) {
    struct ListNode* nodeA = headA;
    struct ListNode* nodeB = headB;
    int n1 = 0, n2 = 0;
    while (nodeA->next) {
        nodeA = nodeA->next;
        n1++;
    }
    while (nodeB->next) {
        nodeB = nodeB->next;
        n2++;
    }
    if (nodeA != nodeB)
        return NULL;
    int mul = abs(n1 - n2);
    struct ListNode* longList = headA, * shortList = headB;
    if (n2 > n1) {
        longList = headB;
        shortList = headA;
    }
    while (mul--) {
        longList = longList->next;
    }
    while (longList != shortList) {
        longList = longList->next;
        shortList = shortList->next;
    }
    return longList;


}
